package se.thoka.twt.client.utils.validation;

import se.thoka.twt.client.utils.ParseUtil;

public class PositiveIntegerValidation implements Validation {
	
	public static final PositiveIntegerValidation INSTANCE_ALLOW_NULL = new PositiveIntegerValidation(true);
	public static final PositiveIntegerValidation INSTANCE_NOT_NULL = new PositiveIntegerValidation(false);
	
	private final boolean allowNull;
	
	public PositiveIntegerValidation(boolean allowNull) {
		this.allowNull = allowNull;
	}

	@Override
	public String getDescription() {
		return ValidationUtil.getStrings().invalidPositiveInteger();
	}
	
	@Override
	public boolean validate(String value) {
		return (value == null && allowNull) || ParseUtil.tryParse(value, -1) >= 0;
	}
}
